package com.example.service.serviceImple;

import com.example.dao.ArticalDao;
import com.example.dao.CommentDao;
import com.example.dao.UserDao;
import com.example.entity.Article;
import com.example.entity.Comment;
import com.example.entity.CommentLike;
import com.example.entity.User;
import com.example.entity.vo.CommentVO;
import com.example.service.CommentService;
import com.example.util.BeanFactroy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 评论服务层实现类
 * @Date: 2022/11/15 16:12
 * @Author: SinkDream
 */
public class CommentServiceImpl implements CommentService {
    private CommentDao commentDao = BeanFactroy.createBean(CommentDao.class);
    private ArticalDao articalDao = BeanFactroy.createBean((ArticalDao.class));

    //存放一级子评论的集合
    private List<CommentVO> tempReply = new ArrayList<>();

    //查询评论
    @Override
    public List<CommentVO> listCommentByArticleId(Long articleId) {

        //查询父级评论
        List<CommentVO> comments = commentDao.findByArticleIdParentId(articleId,Long.parseLong("-1"));

        for (CommentVO comment : comments) {
            Long id = comment.getId();
            //查询一级子评论
            List<CommentVO> childComments = commentDao.findByArticleIdParentId(articleId,id);

            addFirstChildren(articleId,childComments,comment.getNickName());
            comment.setReplyComments(tempReply);
            comment.setReplyCount(tempReply.size());
            tempReply = new ArrayList<>();
        }
        return comments;
    }

    //查询一级子评论
    private void addFirstChildren(Long articleId,List<CommentVO> childComments,String nickName){
        if (childComments.size() > 0){
            for (CommentVO childComment : childComments){
                childComment.setParentNickName(nickName);

                tempReply.add(childComment);
                Long childId = childComment.getId();
                String parentNickname = childComment.getNickName();
                String parentContent = childComment.getContent();
                addSecondChildComment(articleId,childId,parentNickname,parentContent);
            }
        }
    }

    //递归查询二级子评论
    private void addSecondChildComment(Long articleId,Long childId,String parentNickname,String parentContent){
        List<CommentVO> secondChildComments = commentDao.findByArticleIdParentId(articleId,childId);
        if (secondChildComments.size() > 0){
            for (CommentVO secondChildComment : secondChildComments){
                secondChildComment.setParentNickName(parentNickname);
                secondChildComment.setParentContent(parentContent);
                tempReply.add(secondChildComment);
                Long id = secondChildComment.getId();
                String parentNickname2 = secondChildComment.getNickName();
                String parentContent2 = secondChildComment.getContent();
                addSecondChildComment(articleId,id,parentNickname2,parentContent2);
            }
        }
    }

    //添加评论
    @Override
    public int addComment(Comment comment) {
        comment.setCreateTime(new Date());
        Article article = articalDao.getArticleById(comment.getArticleId());
        article.setCommentCount(article.getCommentCount()+1);
        articalDao.updateArticle(article);
        return commentDao.addComment(comment);
    }

    //查询文章评论总数
    @Override
    public int countComment(Long articleId) {
        return commentDao.countComment(articleId);
    }


    @Override
    public void deleteComment(String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Long id = Long.parseLong(ids[i]);
            cascadeDelete(id);
            commentDao.deleteComment(id);
        }
    }

    @Override
    public int addCommentLike(CommentLike commentLike) {
        Comment comment = commentDao.getComment(commentLike.getCommentId());
        comment.setLikes(comment.getLikes()+1);
        commentDao.updateCommentLikes(comment);
        return commentDao.addCommentLike(commentLike);
        //CommentLike commentLike2 = commentDao.getCommentLike(commentLike.getId());
        //Comment comment = commentDao.getComment(commentLike.getCommentId());
        //if (commentLike2 == null){
        //    comment.setLikes(comment.getLikes()+1);
        //    commentDao.updateCommentLikes(comment);
        //    return commentDao.addCommentLike(commentLike);
        //}else {
        //    if (commentLike.getIsLike() == 1){
        //        comment.setLikes(comment.getLikes()+1);
        //    }else {
        //        comment.setLikes(comment.getLikes()-1);
        //    }
        //    commentLike2.setIsLike(commentLike.getIsLike());
        //    return commentDao.updateCommentLike(commentLike2);
        //}
    }

    @Override
    public int removeCommentLike(CommentLike commentLike) {
        Comment comment = commentDao.getComment(commentLike.getCommentId());
        comment.setLikes(comment.getLikes()-1);
        commentDao.updateCommentLikes(comment);
        return commentDao.deleteCommentLike(commentLike);
    }

    @Override
    public List<CommentLike> listCommentLike(Long userId) {
        return commentDao.listCommentLike(userId);
    }

    //递归方法级联删除评论
    private void cascadeDelete(Long parentId){
        List<Comment> comments = commentDao.listCommentByParentId(parentId);
        if(!comments.isEmpty()){
            for (Comment comment : comments) {
                cascadeDelete(comment.getId());
            }
        }
        commentDao.deleteComment(parentId);
    }

}
